package cjj;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/admin/deleteMovie")
public class AdminDeleteMovieServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer adminId = (Integer) request.getSession().getAttribute("adminId");
        if (adminId == null) {
            response.sendRedirect("/admin/login");
            return;
        }

        String movieIdParam = request.getParameter("movieId");
        if (movieIdParam == null) {
            response.getWriter().println("电影ID不能为空");
            return;
        }
        Integer movieId = Integer.valueOf(movieIdParam);

        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/webproject_2025", "root", "root");
             PreparedStatement stmt = conn.prepareStatement("DELETE FROM movies WHERE movie_id = ?")) {
            stmt.setInt(1, movieId);
            int rowsAffected = stmt.executeUpdate();
            if (rowsAffected > 0) {
                response.getWriter().println("电影删除成功");
            } else {
                response.getWriter().println("电影删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            response.getWriter().println("数据库错误");
        }
    }
}